import { createWebHistory, createRouter } from "vue-router";
export {
  setupDynamicRouter,
  MenuRouterFormat
} from './routes'
import { publicRoutes, authentication } from "./routes";
const routes = publicRoutes.concat(authentication);
const envObj = import.meta.env;
const router = createRouter({
  history: createWebHistory(envObj.BASE_URL),
  routes,
});

// 全局路由守卫,承担格式化路由的责任
router.beforeEach(async (to) => {
  if (publicRoutes.some((routeItem) => routeItem.name === to.name) || sessionStorage.getItem(envObj.VITE_TOKEN_KEY)) {
    // 已登录且初始化过
    return true;
  } else {
    // 没登陆过
    if (envObj.DEV) console.log(Date.now(), "用户从未登陆过");
    return { name: "login" };
  }
});
router.onError((e, to, form) => {
  console.error("路由出现错误");
  console.error(e, to, form);
  router.replace({ name: "notFount" });
});
export default router;
